package com.sc.nft.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.ActivityBasics;
import com.sc.nft.entity.PrizeDrawActivityPool;
import com.sc.nft.entity.PrizeDrawActivityRewards;
import com.sc.nft.entity.dto.ActivationRecordDTO;
import com.sc.nft.entity.dto.ActivityBasicsPageDTO;
import com.sc.nft.entity.dto.CreateActivityDTO;
import com.sc.nft.entity.dto.ModificationActivityDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ActivityBasicsService;
import com.sc.nft.service.CarveUpPoolService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * (ActivityBasics)表控制层
 *
 * @author 王伟成
 * @since 2023-09-13 15:58:37
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/activityBasics")
@Api(tags = "竞猜活动")
public class ActivityBasicsAdminController {
    /**
     * 服务对象
     */
    private final ActivityBasicsService activityBasicsService;
    private final CarveUpPoolService carveUpPoolService;


    @PostMapping(value = "/createActivity", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建活动", notes = "创建活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result createActivity(@Valid @RequestBody CreateActivityDTO createActivityDTO) {
        activityBasicsService.createActivity(createActivityDTO);
        return Result.success();
    }

    @PostMapping(value = "/activationRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "活动记录", notes = "活动记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<ActivationRecordVO> activationRecord(@Valid @RequestBody ActivationRecordDTO activationRecordDTO) {
        return PageResult.success(activityBasicsService.activationRecord(activationRecordDTO));
    }

    @PostMapping(value = "/activityBasics", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "活动列表", notes = "活动列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<ActivityBasicsVO> activityBasics(@Valid @RequestBody ActivityBasicsPageDTO activityListDTO) {
        return PageResult.success(activityBasicsService.activityBasics(activityListDTO));
    }

    @PostMapping(value = "/deleteById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除活动", notes = "删除活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteById(@Valid @RequestBody IdRequest idRequest) {
        activityBasicsService.deleteById(idRequest.getId());
        return Result.success();
    }
    @PostMapping(value = "/detailsById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "详情数据", notes = "详情数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<ActivityBasicsDetailsVO> detailsById(@Valid @RequestBody IdRequest idRequest) {
        return Result.success(activityBasicsService.detailsById(idRequest.getId()));
    }


    @PostMapping(value = "/modificationActivity", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改活动", notes = "修改活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result modificationActivity(@Valid @RequestBody ModificationActivityDTO modificationActivityDTO) {
        activityBasicsService.modificationActivity(modificationActivityDTO);
        return Result.success();
    }

    @PostMapping(value = "/unmount", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "上下架", notes = "上下架", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result unmount(@Valid @RequestBody IdRequest idRequest) {
        activityBasicsService.unmount(idRequest.getId());
        return Result.success();
    }

    @PostMapping(value = "/sort", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改排序", notes = "修改排序", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result sort(@Valid @RequestBody SortRequest sortRequest) {
        activityBasicsService.sort(sortRequest.getId(),sortRequest.getSort());
        return Result.success();
    }


    @PostMapping(value = "/getResidualPrizePool", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取奖池剩余奖金", notes = "获取奖池剩余奖金", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result getResidualPrizePool() {
        return Result.success(carveUpPoolService.getResidualPrizePool());
    }


    @PostMapping(value = "/addCarveUpActivity", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增瓜分活动", notes = "新增瓜分活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addCarveUpActivity(@Valid @RequestBody AddCarveUpActivityRequest request) {
        ActivityBasics activityBasics = BeanUtil.copyProperties(request, ActivityBasics.class);
        Assert.isTrue(activityBasics.getStartTime().before(activityBasics.getEndTime()),()-> new GlobalRunTimeException("结束时间必须大于开始时间"));

        List<PrizeDrawActivityRewards> rewardList = BeanUtil.copyToList(request.getPrizeDrawActivityRewardsRequestList(), PrizeDrawActivityRewards.class);

        activityBasicsService.addCarveUpActivity(activityBasics,request.getBonusType(),rewardList,request.getActivityTasks());

        return Result.success();
    }

    @PostMapping(value = "/AddPrizeDrawCctivityPool", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增瓜分活动-奖池配置", notes = "新增瓜分活动-奖池配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result AddPrizeDrawCctivityPool(@Valid @RequestBody List<AddPrizeDrawCctivityPoolBatchRequest> request) {
        activityBasicsService.addPrizeDrawCctivityPool(BeanUtil.copyToList(request, PrizeDrawActivityPool.class));
        return Result.success();
    }


    @PostMapping(value = "/carveUpActivityDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增瓜分活动详情", notes = "新增瓜分活动详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<AdminCarveUpActivityDetailsVO> CarveUpActivityDetails(@Valid @RequestBody IdRequest request) {
        return Result.success(activityBasicsService.carveUpActivityDetailsById(request.getId()));
    }


    @ApiOperation(value = "活动数据-参与数据", notes = "活动数据-参与数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/participationData", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<ParticipationDataVO> participationData(@Valid @RequestBody IdRequest request) {
        return Result.success(activityBasicsService.getPageSecondaryMarketCollection(request.getId()));
    }

    @ApiOperation(value = "查看签到活动数据-获取日参与统计数据", notes = "查看签到活动数据-获取日参与统计数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/dayStatisticalData", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<ParticipationDateDataVO> dayStatisticalData(@Valid @RequestBody IdRequest request) {
        return Result.success(activityBasicsService.dayStatisticalData(request.getId()));
    }

    @ApiOperation(value = "活动数据-中奖明细", notes = "活动数据-中奖明细", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/lotteryDataPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<LotteryDataPageVO> lotteryDataPage(@Valid @RequestBody LotteryDataPageRequest request) {
        return PageResult.success(activityBasicsService.lotteryDataPage(request.getId(),request.getUserTel(),request.getLotteryDrawResultVar(),request.getTypeName(),request.toMp()));
    }

    @ApiOperation(value = "修改奖励是否拦截", notes = "修改奖励是否拦截", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateRewardsStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateRewardsStatus(@Valid @RequestBody UpdateRewardsStatusRequest request) {
        activityBasicsService.updateRewardsStatus(request.getId(),request.getStatus());
        return Result.success();
    }

    @PostMapping(value = "/activityBasicsList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "活动列表不分页", notes = "活动列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<ActivityBasicsListVO>> activityBasicsList() {
        return PageResult.success(activityBasicsService.activityBasicsList());
    }

}
